// gjw/pages/doubao/my.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menu: ['全部', '待审核', '未通过', '已通过'],
    activeIndex: 0,
    index: 0,
    page:1,
    dblist:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
		var siteUrl = wx.getStorageSync('siteUrl'),
		activeIndex = that.data.activeIndex

    that.setData({
      siteUrl: siteUrl
    })
    that.identy()
    if (activeIndex == 0) {
      that.getAll()
    } else if (activeIndex == 1) {
      that.one()
    } else if (activeIndex == 2) {
      that.two()
    } else {
      that.three()
    }
		app.util.request({
			url: 'entry/wxapp/primoney',
			success: function (res) {
				
				that.setData({
					priupfee: res.data.res.upfee,
					pritopfee: res.data.res.topfee,
				})
			}
		})
		getApp().util.request({
			url: 'entry/wxapp/Repucostiwantut',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				if (res.data.code == 200) {
					that.setData({
						iwantrestups: res.data.restups,
						iwantresttops: res.data.resttops
					})
				}
			}
		})
  },
  // 判断身份
  identy: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    
    app.util.request({
      url: 'entry/wxapp/Iscustomers',
      data: { openid: openid },
      success: function (res) {
        
        if (res.data.code == 204) {    //商家审核通过  商家
          that.setData({
						zt: 1,
						user_identity: 2
          })
        } else if (res.data.code == 200 ||res.data.code == 201 || res.data.code == 500 || res.data.code == 202) {    //商家审核未通过  用户
          that.setData({
            zt: 0
          })
        }
      }
    })
  },

  tabClick: function (e) {
    
    this.setData({
      activeIndex: e.currentTarget.dataset.index,
      page:1,
    })
    this.tab()
  },
  tab:function(e){
    var activeIndex = this.data.activeIndex
    if (activeIndex == 0) {
      this.setData({
        dbone: [],
        dbtwo: [],
        dbthree: [],
        dblist: []
      })
      this.getAll()
    } else if (activeIndex == 1) {
      this.setData({
        dbone: [],
        dbtwo: [],
        dbthree: [],
        dblist: []
      })
      this.one()
    } else if (activeIndex == 2) {
      this.setData({
        dbone: [],
        dbtwo: [],
        dbthree: [],
        dblist: []
      })
      this.two()
    } else {
      this.setData({
        dbone: [],
        dbtwo: [],
        dbthree: [],
        dblist: []
      })
      this.three()
    }

  },

  react: function (e) {
    var activeIndex = this.data.activeIndex
    if (activeIndex == 0) {
      this.getAll()
    } else if (activeIndex == 1) {
      this.one()
    } else if (activeIndex == 2) {
      this.two()
    } else {
      this.three()
    }

  },
  waite: function (e) {
    wx.showModal({
      title: '提示',
      content: '您的定制正在审核中请耐心等待哦~',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          
        }
      }
    })
  },
  pass: function (e) {
    wx.showModal({
      title: '提示',
      content: '您的定制审核未通过，发布费会在1~3个工作日退回，请注意查看',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          
        }
      }
    })
  },
  top: function (e) {//好像没什么用了
    var db_id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '本次刷新付款后可将该帖子再次推荐置顶，确定继续吗？',
      success: function (res) {
        if (res.confirm) {
          
        } else if (res.cancel) {
          
        }
      }
    })
  },

  // 获取全部
  getAll: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    var page = that.data.page
    var dblist = that.data.dblist
    app.util.request({
      url: 'entry/wxapp/priall',
      data: { openid: openid,page:page },
      success: function (res) {
        
        if (res.data.code == 200) {
          for (let i in res.data.res){
            res.data.res[i].img = res.data.res[i].img.split(',')
          }
          dblist = dblist.concat(res.data.res.reverse())
          that.setData({
            dblist: dblist.sort(function(a,b){
              return Date.parse(b.time)-Date.parse(a.time)
            }),
            page:page+1
          })
          
        } else if (res.data.code == 500) {
          // that.setData({
          //   dblist: []
          // })
        }
      },
    })
  },

  // 待审核
  one: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    var page = that.data.page
    var dbone = that.data.dbone
    app.util.request({
      url: 'entry/wxapp/Prinsh',
      data: { openid: openid, page: page },
      success: function (res) {
        
        if (res.data.code == 200) {
          for (let i in res.data.res) {
            res.data.res[i].img = res.data.res[i].img.split(',')
          }
          dbone = dbone.concat(res.data.res.reverse())
          that.setData({
            dbone: dbone.sort(function (a, b) {
              return Date.parse(b.time) - Date.parse(a.time)
            }),
            page: page + 1
          })

        } else if (res.data.code == 500) {
          // that.setData({
          //   dblist: []
          // })
        }
      },
    })
  },


  // 未通过
  two: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    var page = that.data.page
    var dbtwo = that.data.dbtwo
    app.util.request({
      url: 'entry/wxapp/Prijsh',
      data: { openid: openid, page: page },
      success: function (res) {
        
        if (res.data.code == 200) {
          for (let i in res.data.res) {
            res.data.res[i].img = res.data.res[i].img.split(',')
          }
          dbtwo = dbtwo.concat(res.data.res.reverse())
          that.setData({
            dbtwo: dbtwo.sort(function (a, b) {
              return Date.parse(b.time) - Date.parse(a.time)
            }),
            page: page + 1
          })

        } else if (res.data.code == 500) {
          // that.setData({
          //   dblist: []
          // })
        }
      },
    })
  },

  // 已通过
  three: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    var page = that.data.page
    var dbthree = that.data.dbthree
    app.util.request({
      url: 'entry/wxapp/Priysh',
      data: { openid: openid, page: page },
      success: function (res) {
        
        if (res.data.code == 200) {
          for (let i in res.data.res) {
            res.data.res[i].img = res.data.res[i].img.split(',')
          }
          dbthree = dbthree.concat(res.data.res.reverse())
          that.setData({
            dbthree: dbthree.sort(function (a, b) {
              return Date.parse(b.time) - Date.parse(a.time)
            }),
            page: page + 1
          })

        } else if (res.data.code == 500) {
          // that.setData({
          //   dblist: []
          // })
        }
      },
    })
  },


  delete: function (e) {
    var that = this
    
    var id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '是否确定删除该条定制？',
      success: function (res) {
        if (res.confirm) {
          
          app.util.request({
            url: 'entry/wxapp/Pridelete',
            data: { id: id },
            success: function (res) {
              
              if (res.data.code == 200) {
                that.getyData()
                wx.showToast({
                  title: '删除成功',
                })
                that.two()
                that.three()
              }
            }
          })
        } else if (res.cancel) {
          
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.tab()
    // wx.stopPullDownRefresh()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.react()
  },
  Customized: function (e) {
    var type = e.currentTarget.dataset.type
    if (this.data.zt == 1) {
      wx.navigateTo({
        url: 'post?type=' + 1,
      })
    } else if (this.data.zt == 0) {
      wx.navigateTo({
        url: 'post?type=' + 0,
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
	tuijian: function (e) {
		var that = this
		var res_id = e.currentTarget.dataset.id
		var openid = wx.getStorageSync('openid')
		wx.showModal({
			title: '提示',
			content: '是否确认推荐本条定制？',
			success: function (res) {
				if (res.confirm) {
					if(that.data.priupfee>0) {
						if (that.data.iwantrestups > 0) {
							app.util.request({
								url: 'entry/wxapp/priupsuccess',
								data: {
									id: res_id,openid:openid,upu:1
								},
								success: function (res) {
									if (res.data.code == 200) {
										that.data.dblist[e.currentTarget.dataset.index].upfee = 1
										that.setData({
											dblist: that.data.dblist
										})
										wx.showToast({
											icon: "none",
											title: '推荐成功',
										})
									} else {
										wx.showToast({
											icon: "none",
											title: '推荐失败',
										})
									}
								}
							})
						}else{
							app.util.request({
								url: 'entry/wxapp/priuppay',
								data: {
									openid: openid,
								},
								success: function (res) {
									wx.requestPayment({
										'timeStamp': res.data.timeStamp,
										'nonceStr': res.data.nonceStr,
										'package': res.data.package,
										'signType': 'MD5',
										'paySign': res.data.paySign,
										'success': function (res) {
											app.util.request({
												url: 'entry/wxapp/priupsuccess',
												data: {
													id: res_id
												},
												success: function (rest) {
													if (rest.data.code == 200) {
														that.data.dblist[e.currentTarget.dataset.index].upfee = 1
														that.setData({
															dblist: that.data.dblist
														})
														wx.showToast({
															icon: "none",
															title: '推荐成功',
														})
													} else {
														wx.showToast({
															icon: "none",
															title: '推荐失败',
														})
													}
												}
											})

										},
										'fail': function (res) {
											;
											wx.showToast({
												title: '支付失败',
												icon: "loading",
											})
										}
									})
								}
							})
						}
					}
					else  {
						app.util.request({
							url: 'entry/wxapp/priupsuccess',
							data: {
								id: res_id
							},
							success: function (res) {
								if (res.data.code == 200) {
									that.data.dblist[e.currentTarget.dataset.index].upfee = 1
									that.setData({
										dblist: that.data.dblist
									})
									wx.showToast({
										icon: "none",
										title: '推荐成功',
									})
								} else {
									wx.showToast({
										icon: "none",
										title: '推荐失败',
									})
								}
							}
						})

					}

				} else if (res.cancel) {
					
				}
			}
		})
	},
	tops: function (e) {
		var that = this
		var res_id = e.currentTarget.dataset.id
		var cash = Number(that.data.cash)
		var openid = wx.getStorageSync('openid')
		wx.showModal({
			title: '提示',
			content: '是否确认置顶本条抖宝？',
			success: function (res) {
				if (res.confirm) {
					if (that.data.pritopfee > 0) {
						if (that.data.iwantresttops > 0) {
							app.util.request({
								url: 'entry/wxapp/pritopsuccess',
								data: {
									id: res_id,openid:openid,topu:1
								},
								success: function (res) {

									if (res.data.code == 200) {

										wx.showToast({
											icon: "none",
											title: '置顶成功',
										})
									} else {
										wx.showToast({
											icon: "none",
											title: '置顶失败',
										})
									}
								}
							})
						}else{
							app.util.request({
								url: 'entry/wxapp/pritoppay',
								data: {
									openid: openid,
								},
								success: function (res) {
									
									wx.requestPayment({
										'timeStamp': res.data.timeStamp,
										'nonceStr': res.data.nonceStr,
										'package': res.data.package,
										'signType': 'MD5',
										'paySign': res.data.paySign,
										'success': function (res) {
											app.util.request({
												url: 'entry/wxapp/pritopsuccess',
												data: {
													id: res_id
												},
												success: function (rest) {
													if (rest.data.code == 200) {
														
														wx.showToast({
															icon: "none",
															title: '置顶成功',
														})
													} else {
														wx.showToast({
															icon: "none",
															title: '置顶失败',
														})
													}
												}
											})

										},
										'fail': function (res) {
											;
											wx.showToast({
												title: '支付失败',
												icon: "loading",
											})
										},
										'complete': function (res) {
											;
											wx.showToast({
												title: '取消支付',
												icon: "loading",
											})
										}
									})
								}
							})
						}
					}
					else {
						app.util.request({
							url: 'entry/wxapp/pritopsuccess',
							data: {
								id: res_id
							},
							success: function (res) {
								
								if (res.data.code == 200) {
									
									wx.showToast({
										icon: "none",
										title: '置顶成功',
									})
								} else {
									wx.showToast({
										icon: "none",
										title: '置顶失败',
									})
								}
							}
						})
					}
				} else if (res.cancel) {
					
				}
			}
		})
	},
	tuijians: function (e) {
		var that = this
		var res_id = e.currentTarget.dataset.id
		var openid = wx.getStorageSync('openid')
		wx.showModal({
			title: '提示',
			content: '是否确认推荐本条定制？',
			success: function (res) {
				if (res.confirm) {
					if (that.data.priupfee > 0) {
						if (that.data.iwantrestups > 0) {
							app.util.request({
								url: 'entry/wxapp/priupsuccess',
								data: {
									id: res_id,openid:openid,upu:1
								},
								success: function (res) {

									if (res.data.code == 200) {
										that.data.dbthree[e.currentTarget.dataset.index].upfee = 1
										that.setData({
											dbthree: that.data.dbthree
										})
										wx.showToast({
											icon: "none",
											title: '推荐成功',
										})
									} else {
										wx.showToast({
											icon: "none",
											title: '推荐失败',
										})
									}
								}
							})
						}else{
							app.util.request({
								url: 'entry/wxapp/priuppay',
								data: {
									openid: openid,
								},
								success: function (res) {
									wx.requestPayment({
										'timeStamp': res.data.timeStamp,
										'nonceStr': res.data.nonceStr,
										'package': res.data.package,
										'signType': 'MD5',
										'paySign': res.data.paySign,
										'success': function (res) {
											app.util.request({
												url: 'entry/wxapp/priupsuccess',
												data: {
													id: res_id
												},
												success: function (rest) {
													if (rest.data.code == 200) {
														that.data.dbthree[e.currentTarget.dataset.index].upfee = 1
														that.setData({
															dbthree: that.data.dbthree
														})
														wx.showToast({
															icon: "none",
															title: '推荐成功',
														})
													} else {
														wx.showToast({
															icon: "none",
															title: '推荐失败',
														})
													}
												}
											})

										},
										'fail': function (res) {
											;
											wx.showToast({
												title: '支付失败',
												icon: "loading",
											})
										},
										'complete': function (res) {
											;
											wx.showToast({
												title: '取消支付',
												icon: "loading",
											})
										}
									})
								}
							})
						}
					}
					else {
						app.util.request({
							url: 'entry/wxapp/priupsuccess',
							data: {
								id: res_id
							},
							success: function (res) {
								
								if (res.data.code == 200) {
									that.data.dbthree[e.currentTarget.dataset.index].upfee = 1
									that.setData({
										dbthree: that.data.dbthree
									})
									wx.showToast({
										icon: "none",
										title: '推荐成功',
									})
								} else {
									wx.showToast({
										icon: "none",
										title: '推荐失败',
									})
								}
							}
						})

					}

				} else if (res.cancel) {
					
				}
			}
		})
	},
	topss: function (e) {
		var that = this
		var res_id = e.currentTarget.dataset.id
		var cash = Number(that.data.cash)
		var openid = wx.getStorageSync('openid')
		wx.showModal({
			title: '提示',
			content: '是否确认置顶本条抖宝？',
			success: function (res) {
				if (res.confirm) {
					if (that.data.pritopfee > 0) {
						if (that.data.iwantresttops > 0) {
							app.util.request({
								url: 'entry/wxapp/pritopsuccess',
								data: {
									id: res_id,openid:openid,topu:1
								},
								success: function (res) {

									if (res.data.code == 200) {

										wx.showToast({
											icon: "none",
											title: '置顶成功',
										})
									} else {
										wx.showToast({
											icon: "none",
											title: '置顶失败',
										})
									}
								}
							})
						} else {
							app.util.request({
								url: 'entry/wxapp/pritoppay',
								data: {
									openid: openid,
								},
								success: function (res) {
									wx.requestPayment({
										'timeStamp': res.data.timeStamp,
										'nonceStr': res.data.nonceStr,
										'package': res.data.package,
										'signType': 'MD5',
										'paySign': res.data.paySign,
										'success': function (res) {
											app.util.request({
												url: 'entry/wxapp/pritopsuccess',
												data: {
													id: res_id
												},
												success: function (rest) {
													if (rest.data.code == 200) {

														wx.showToast({
															icon: "none",
															title: '置顶成功',
														})
													} else {
														wx.showToast({
															icon: "none",
															title: '置顶失败',
														})
													}
												}
											})

										},
										'fail': function (res) {
											;
											wx.showToast({
												title: '支付失败',
												icon: "loading",
											})
										}
									})
								}
							})
						}
					}
					else {
						app.util.request({
							url: 'entry/wxapp/pritopsuccess',
							data: {
								id: res_id
							},
							success: function (res) {
								
								if (res.data.code == 200) {
									
									wx.showToast({
										icon: "none",
										title: '置顶成功',
									})
								} else {
									wx.showToast({
										icon: "none",
										title: '置顶失败',
									})
								}
							}
						})



					}





				} else if (res.cancel) {
					
				}
			}
		})
	}
})